<template>
  <div style="background-color: #f3f0f0; padding: 20px">
    <!--  首页顶部经营数据 -->
    <div class="top" style="background-color: white; padding: 20px">
      <!--  首部    -->
      <div class="header">
        <!-- 标题放于左侧-->
        <div>
          <i
            class="el-icon-s-shop"
            style="font-size: 20px; vertical-align: -2px; margin-right: 4px"
          ></i>
          <span style="font-size: 16px; font-weight: bold">经营数据</span>
        </div>
        <!-- 时间选择器区域，放在右侧 -->
        <div class="block">
          <span>选择时间：</span>
          <el-date-picker
            v-model="value1"
            type="daterange"
            align="right"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="pickerOptions"
          >
          </el-date-picker>
        </div>
      </div>
      <div
        style="
          width: 100%;
          border-bottom: 2px solid #ebeef5;
          margin-bottom: 12px;
        "
      ></div>
      <!--  经营数据    -->
      <!--  Layout 布局  gutter栅格间隔、、span 栅格占据的列数 -->
      <div class="wrapper">
        <el-row :gutter="60" style="height: 100px">
          <el-col :span="6">
            <div class="model" style="background: #56cb61">
              <div class="income">
                <p class="wenzi1">今日订单总额（元）</p>
                <p class="wenzi2">¥ {{ this.todayPrice }}</p>
              </div>
              <div class="icons">
                <i class="el-icon-shopping-cart-1"></i>
              </div>
            </div>
          </el-col>
          <el-col :span="6">
            <div class="model" style="background: #8d54e8">
              <div class="income">
                <p class="wenzi1">今日订单总数</p>
                <p class="wenzi2">{{ this.todayOrders }}</p>
              </div>
              <div class="icons">
                <i class="el-icon-tickets"></i>
              </div>
            </div>
          </el-col>
          <el-col :span="6">
            <div class="model" style="background: #5490fd">
              <div class="income">
                <p class="wenzi1">今日新增用户数</p>
                <p class="wenzi2">{{ this.dayUser }}</p>
              </div>
              <div class="icons">
                <i class="el-icon-user"></i>
              </div>
            </div>
          </el-col>
          <el-col :span="6">
            <div class="model" style="background: #f57619">
              <div class="income">
                <p class="wenzi1">今日新增商户</p>
                <p class="wenzi2">{{ this.dayBusinessUser }}</p>
              </div>
              <div class="icons">
                <i class="el-icon-user-solid"></i>
              </div>
            </div>
          </el-col>
        </el-row>
      </div>
    </div>

    <!--  中间部分-->
    <!-- echarts使用
      1、引入 echarts
      2、定义饼图容器：
      3、初始化饼图： 挂载，执行initCharts
      首先使用echarts.init(this.$refs.orderChart)初始化饼图，并赋值给orderChart变量。
      然后，创建一个option对象，用于定义饼图的样式和数据。最后，使用
      orderChart.setOption(option)将option对象应用到饼图上。

      注：ref属性用于引用DOM元素，以便在Vue实例中访问它们。
      this.$refs.orderChart 就是访问这个引用的方式，
      它返回 ref="orderChart" 对应的 DOM 元素或组件的引用。
     -->
    <div class="middle">
      <div class="block" style="flex: 1; margin-left: 0">
        <div class="block-header">
          <i
            class="el-icon-date"
            style="font-size: 20px; margin-right: 4px"
          ></i>
          <h3 class="h3">订单统计</h3>
        </div>
        <div class="line"></div>
        <div ref="orderChart" class="bingtu"></div>
      </div>
      <div class="block" style="flex: 1">
        <div class="block-header">
          <i
            class="el-icon-date"
            style="font-size: 20px; margin-right: 4px"
          ></i>
          <h3 class="h3">救援任务统计</h3>
        </div>
        <div class="line"></div>
        <div ref="taskChart" class="huanbingtu"></div>
      </div>
      <div class="block" style="flex: 2">
        <div class="block-header">
          <i
            class="el-icon-date"
            style="font-size: 20px; margin-right: 4px"
          ></i>
          <h3 class="h3">用户概况</h3>
        </div>
        <div class="line"></div>
        <div class="stats">
          <div class="chunk">
            <div class="one">
              <div>商户数</div>
              <i class="el-icon-question" @click="answer"></i>
            </div>
            <div class="two">
              <div class="two-font">{{ this.businessUser }}</div>
            </div>
          </div>
          <div class="chunk">
            <div class="one">
              <div>新增用户数</div>
              <i class="el-icon-question" @click="answer"></i>
            </div>
            <div class="two">
              <div class="two-font">{{ this.dayUser }}</div>
            </div>
          </div>
          <div class="chunk">
            <div class="one">
              <div>累积用户数</div>
              <i class="el-icon-question" @click="answer"></i>
            </div>
            <div class="two">
              <div class="two-font">1000</div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!--底部柱状图-->
    <div class="bottom">
      <div class="bottom-font">注册用户分析</div>
      <div ref="userChart" class="zhu"></div>
    </div>
  </div>
</template>

<script>
//引入echarts
import * as echarts from "echarts";
import { listOrdersCount } from "@/api/carService/ordersCount";
import { todayDingdan } from "@/api/carService/todayDingdan";
import { todayMerchant } from "@/api/carService/todayMerchant";

export default {
  data() {
    return {
      // 日期选择器
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      value1: "",

      //今日订单收入
      dayMoney: 666,
      //今日订单数量
      dayDingDan: 66,
      //今日新增用户数
      dayUser: 66,
      // 今日新增商户
      dayBusinessUser:'',
      // 商户总数
      businessUser:'',

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        createTime: null,
      },
      // 饼图订单统计
      weixiu: "",
      xiche: "",
      baoyang: "",

      // 今日订单信息
      todayPrice: "",
      todayOrders: "",
    };
  },
  mounted() {
    // 初始化图表实例
    this.initCharts();
    // 初始化用户统计柱状图
    this.getList();
    //  获取订单信息
    this.getTodayDingdan();
    //  获取商户数量信息
    this.getTodayMerchant();
  },
  methods: {
    // 初始化订单统计饼图
    initCharts() {
      // 初始化救援任务统计环形图
      let taskChart = echarts.init(this.$refs.taskChart);
      let taskOption = {
        tooltip: {
          trigger: "item",
        },
        // 配置图例
        legend: {
          orient: "vertical",
          left: "right",
          top: "center",
          data: ["待处理", "已处理"],
        },
        series: [
          {
            name: "任务统计",
            type: "pie",
            // 圆环的内半径和外半径
            radius: ["30%", "60%"],
            // 颜色
            color: ["#A0F3C2", "#F3EF9E"], // 使用颜色数组
            // 是否开启防止标签重叠
            avoidLabelOverlap: false,
            // 隐藏扇区的文字
            label: {
              show: false,
              position: "center",
            },
            // 隐藏扇区的指引线
            labelLine: {
              show: false,
            },

            // 鼠标悬浮时候的样式
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },

            data: [
              { value: 120, name: "待处理" },
              { value: 274, name: "已处理" },
            ],
          },
        ],
      };
      taskChart.setOption(taskOption);

      // 初始化注册用户分析柱状图
      let userChart = echarts.init(this.$refs.userChart);
      let userOption = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
        },
        grid: {
          left: "12%", // 调整grid的left属性，给图形组件留出空间
          right: "5%",
          bottom: "5%",
          containLabel: true,
        },
        graphic: [
          {
            type: "line",
            shape: {
              x1: 100,
              y1: 40, // 线的起点（左上角）
              x2: 100,
              y2: 360, // 线的终点（根据图表高度预估）
            },
            style: {
              stroke: "#000", // 线条颜色
              lineWidth: 1, // 线条宽度
            },
          },
        ],
        xAxis: {
          type: "category",
          data: [
            "1号",
            "2号",
            "3号",
            "4号",
            "5号",
            "6号",
            "7号",
            "8号",
            "9号",
            "10号",
            "11号",
            "12号",
            "13号",
            "14号",
            "15号",
            "16号",
            "17号",
            "18号",
            "19号",
            "20号",
            "21号",
            "22号",
            "23号",
            "24号",
            "25号",
            "26号",
            "27号",
            "28号",
            "29号",
            "30号",
          ],
          axisLabel: {
            interval: 0, // 显示全部标签
          },
        },
        yAxis: [
          {
            type: "value",
            axisLabel: {
              formatter: "{value} ", // y轴上单位为空或不需要特别指出
            },
            splitLine: {
              show: true, // 可选，根据需要决定是否显示分割线
            },
            min: 0,
            max: 250,
            interval: 50, // 刻度间隔
          },
        ],
        series: [
          {
            type: "bar",
            data: [
              100, 120, 80, 130, 90, 240, 190, 90, 50, 70, 110, 130, 90, 230,
              210, 80, 130, 90, 70, 110, 130, 220, 80, 90, 110, 130, 90, 220,
              70, 110,
            ],
            itemStyle: {
              color: "#5A89EC",
            },
          },
        ],
      };
      userChart.setOption(userOption);
    },
    answer() {
      this.$message({
        type: "success",
        message: "点击了问号图标",
      });
    },
    // 获取饼图的数据
    getList() {
      listOrdersCount(this.queryParams).then((response) => {
        // 获取饼图中数据信息  然后更新
        this.weixiu = response.rows[0].weixiu;
        this.baoyang = response.rows[0].baoyang;
        this.xiche = response.rows[0].xiche;
        this.updateBingChart();
      });
    },
    // 初始化饼图
    updateBingChart() {
      let orderChart = echarts.init(this.$refs.orderChart);
      let option = {
        // 用于显示鼠标悬停时的信息
        tooltip: {
          trigger: "item",
        },
        // 配置图例
        legend: {
          orient: "vertical",
          left: "right",

          top: "center",
          data: ["维修类", "洗车类", "保养类"],
        },
        series: [
          {
            name: "订单类型",
            type: "pie",
            radius: "46%",
            data: [
              { value: this.weixiu, name: "维修类" },
              { value: this.xiche, name: "洗车类" },
              { value: this.baoyang, name: "保养类" },
            ],
            // 隐藏扇区的指引线
            labelLine: {
              show: false,
            },
            // 隐藏扇区的文字
            label: {
              show: false,
            },
          },
        ],
        // 鼠标悬浮时候的样式
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: "rgba(0, 0, 0, 0.5)",
          },
        },
      };
      orderChart.setOption(option);
      
      // 监听饼图的点击事件
      orderChart.on("click", () => {
        this.$router.push({ path: "/carService9/ordersCount" });
      });
    },
    // 获取今日订单信息
    getTodayDingdan() {
      todayDingdan(this.queryParams).then((response) => {
        // console.log(response);
        this.todayPrice = response.totalPrice;
        this.todayOrders = response.totalOrders;
      });
    },
    // 获取商户信息
    getTodayMerchant(){
      todayMerchant(this.queryParams).then((response) => {
        this.dayBusinessUser = response[0];
        this.businessUser = response[1];
      });
    }
  },
};
</script>

<style lang="scss">
/* 顶部 */
.top {
  height: 240px;
  .header {
    margin-bottom: 12px;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .model {
    height: 120px;
    width: 280px;
    margin-top: 10px;
    color: white;
    display: flex;
    justify-content: center;
    align-items: center;

    .income {
      .wenzi1 {
        font-size: 16px;
      }
      .wenzi2 {
        font-size: 20px;
        font-weight: bold;
      }
    }
    .icons {
      margin-left: 40px;
      font-size: 42px;
    }
  }
}

.middle {
  display: flex;
  justify-content: space-around;
  margin-top: 10px;

  .block {
    text-align: left;
    background-color: #fff;
    border: #ccc solid 1px;
    padding: 20px;
    margin-left: 20px;
    margin-top: 20px;
    margin-bottom: 20px;
    position: relative;
    height: 280px;

    .block-header {
      display: flex;
      /* 控制交叉轴上的对齐方式，即垂直方向 */
      align-content: center;
      justify-content: flex-start;
      margin-left: 10px;

      .h3 {
        /* 移除默认的 margin，以避免不必要的间距 */
        margin: 0;
        margin-left: 6px;
      }
    }

    .bingtu {
      position: absolute;
      width: 100%;
      height: 300px;
      left: -50px;
      top: 20px;
    }

    .huanbingtu {
      position: absolute;
      width: 100%;
      height: 240px;
      left: -50px;
      top: 50px;
    }
  }

  // 横线
  .line {
    width: 100%;
    height: 1px;
    background-color: #ccc;
    margin: 10px auto;
    margin-top: 15px;
  }
  .stats {
    margin: 10px 0;
    display: flex;
    justify-content: space-between;
    align-content: center;
    .chunk {
      margin-top: 30px;
      border-radius: 8px;
      border: #ccc solid 1px;
      padding: 12px;
      height: 140px;
      .one {
        width: 160px;
        display: flex;
        font-size: 16px;
        position: relative;
        .el-icon-question {
          position: absolute;
          font-size: 18px;
          right: 4px;
          margin-top: 3px;
        }
      }
      .two {
        font-size: 32px;
        margin-top: 40px;
      }
    }
  }
}

.bottom {
  margin-top: 10px;
  background-color: #fff;
  .bottom-font {
    writing-mode: vertical-lr; /* 文字书写方向 */
    margin-left: 50px;
    letter-spacing: 16px;
    position: absolute;
    bottom: 120px;
  }
  .zhu {
    width: 100%;
    height: 400px;
  }
}
</style>
